package com.cz.fra.android;


import com.cz.fra.android.logic.AuthLogic;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener{
	private ImageView addReportImage;
	private ImageView viewReportImage;
    private String userName;
    public static boolean isFromAddReport = true;
    public static String isFirstLogin = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        userName = getIntent().getStringExtra("username");
        initView();
        Config.getInstance().readConfig();
        FishReportApplication.getInstance().addActivities(this);
    }
    
    private void initView() {
    	addReportImage = (ImageView) findViewById(R.id.add_report);
    	viewReportImage = (ImageView) findViewById(R.id.view_report);
		addReportImage.setOnClickListener(this);
		viewReportImage.setOnClickListener(this);
    }
    
	@Override
	public void onClick(View v) {
		isFirstLogin = AuthLogic.getInstance(this).getAuthInfo().getIsFirstLogin();
		Intent intent = new Intent();
		switch (v.getId()) {
		    case R.id.add_report:
		    	if (isFirstLogin == null) {
		    		finish();
		    		intent.setClass(MainActivity.this, LoginActivity.class);
				    startActivity(intent);
		    	} else {
		    		finish();
		    		intent.setClass(MainActivity.this, AddReportActivity.class);
		    		intent.putExtra("username", userName);
		    		startActivity(intent);
		    	}
			    break;
		    case R.id.view_report:
		    	if (isFirstLogin == null) {
		    		finish();
		    		intent.setClass(MainActivity.this, LoginActivity.class);
				    startActivity(intent);
		    	} else {
		    		finish();
		    		intent.setClass(MainActivity.this, ViewReportActivity.class);
		    		startActivity(intent);
		    	}
			    break;
	        default:
				break;
		}
	}
    
	@Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  
            exitDialog();  
            return true;  
        }  
        return true;  
    }

	private void exitDialog() {
		AlertDialog.Builder builder = new Builder(this);  
        builder.setMessage("Confirm exit?");  
        builder.setTitle("Alert");  
        builder.setPositiveButton("YES",  
        new android.content.DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();  
                FishReportApplication.getInstance().exit();
            }  
        });  
        builder.setNegativeButton("NO",  
        new android.content.DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();  
            }  
        });  
        builder.create().show();  
	}

	@Override
	protected void onDestroy() {
		
		super.onDestroy();
	}  
	
}
